class Solution {
public:
    vector<int> ret;
    vector<vector<int>> ans;

    void dfs(int cur,vector<bool>& vis,vector<vector<int>>& graph)
    {
        if(cur == graph.size() - 1)
        {
            ans.push_back(ret);
            return;
        }

        for(int i = 0;i < graph[cur].size();i++)
        {
            int next = graph[cur][i];
            if(vis[next]) continue;
            vis[next] = true;
            ret.push_back(next);
            dfs(next,vis,graph);
            ret.pop_back();
            vis[next] = false;
        }
    }

    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        vector<bool> vis(graph.size(),false);
        ret.push_back(0);
        dfs(0,vis,graph);
        return ans;
    }
};